package com.wxx.socket;

import com.wxx.socket.server.SocketChannelHandler;
import com.wxx.socket.server.WebSocketChannelHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

public class BroadcastUtil {
    /**
     * 广播消息到所有的socket链接人数
     * @param content 广播的内容
     * @return 返回广播成功的人数
     */
    public static int broadcastToSocket(String content) {
        SocketChannelHandler.socketGroup.writeAndFlush(content);
        return SocketChannelHandler.socketGroup.size();
    }

    /**
     * 广播消息到所有的socket链接人数
     * @param content 广播的内容
     * @return 返回广播成功的人数
     */
    public static int broadcastToWebSocket(String content) {
        WebSocketChannelHandler.webSocketGroup.writeAndFlush(new TextWebSocketFrame(content));
        return WebSocketChannelHandler.webSocketGroup.size();
    }

    public static int broadcastToAll(String content) {
        int socketCount = broadcastToSocket(content);
        int webSocketCount = broadcastToWebSocket(content);
        return socketCount + webSocketCount;
    }
}
